#ifndef __IIC_H
#define __IIC_H

#ifdef APM32E10X_HD
	#include "apm32e10x_gpio.h"
	#include "apm32e10x_rcm.h"
	#include "apm32e10x_i2c.h"
	#include "apm32e10x_misc.h"
	
#elif defined AT32F403AVGT7
	#include "at32f403a_407_gpio.h"	
	#include "at32f403a_407_crm.h" 
	#include "at32f403a_407_i2c.h" 
	#include "at32f403a_407_misc.h" 
	#include "at32f403a_407_dma.h"
	
#else
	#include "apm32f10x_gpio.h"
	#include "apm32f10x_rcm.h"
	#include "apm32f10x_i2c.h"
	#include "apm32f10x_misc.h"
#endif

#include "stdio.h"
#include "BoardData.h"

// IIC 引脚 // 
#define IIC2_PIN_RTC                   Led2_RTC
#define IIC2_PinAF_RTC                 RCM_APB2_PERIPH_AFIO
#define IIC2_PinPort                   Led2_PinPort
// SCL = PB10; SDA = PB11
#define IIC2_SDA_PinId                 Led2_3PinId
#define IIC2_SCL_PinId                 Led2_2PinId


// IIC配置
#define IIC_RTC                        RCM_APB1_PERIPH_I2C2
#define IIC_ID                         I2C2
// 主设备地址
#define My_Addr                        0x50
// 从设备写地址
#define OLed_Tx_Addr                   0x78

// I2C中断
#define I2Cn_EV_IRQn                   I2C2_EV_IRQn     
#define I2Cn_EV_IRQHandler             I2C2_EV_IRQHandler

// I2C的DMA参数
#define IIC2_DMA_RTC                   RCM_AHB_PERIPH_DMA1
#define IIC_RX_ADDRESS                 (uint32_t)(I2C2_BASE + 0x10)    
#define IIC_TX_ADDRESS                 (uint32_t)(I2C2_BASE + 0x10)

#define IIC_DMA_RX_Channel             DMA1_Channel5
#define IIC_DMA_TX_Channel             DMA1_Channel4  

#define I2C_Rx_DMA_Chanel_IRQn         DMA1_Channel5_IRQn
#define I2C_Tx_DMA_Chanel_IRQn         DMA1_Channel4_IRQn

#define I2C_Rx_DMAChanel_IRQHandler    DMA1_Channel5_IRQHandler
#define I2C_Tx_DMAChanel_IRQHandler    DMA1_Channel4_IRQHandler

#define I2C_Rx_DMA_INT_TC              DMA1_INT_FLAG_TC5
#define I2C_Tx_DMA_INT_TC              DMA1_INT_FLAG_TC4 

#define I2C_Rx_DMA_FLAG_TC             DMA1_FLAG_TC5
#define I2C_Tx_DMA_FLAG_TC             DMA1_FLAG_TC4 

#define I2C_TimeOut                    250

// 常数定义:
#ifdef AT32F403AVGT7
// 事件检测：
#define I2C_EVENT_CHECK_NONE             ((uint32_t)0x00000000)
#define I2C_EVENT_CHECK_ACKFAIL          ((uint32_t)0x00000001) 
#define I2C_EVENT_CHECK_STOP             ((uint32_t)0x00000002) 
// I2C错误码：
typedef enum{
  I2C_OK = 0,          /*!< no error */
  I2C_ERR_STEP_1,      /*!< step 1 error */
  I2C_ERR_STEP_2,      /*!< step 2 error */
  I2C_ERR_STEP_3,      /*!< step 3 error */
  I2C_ERR_STEP_4,      /*!< step 4 error */
  I2C_ERR_STEP_5,      /*!< step 5 error */
  I2C_ERR_STEP_6,      /*!< step 6 error */
  I2C_ERR_STEP_7,      /*!< step 7 error */
  I2C_ERR_STEP_8,      /*!< step 8 error */
  I2C_ERR_STEP_9,      /*!< step 9 error */
  I2C_ERR_STEP_10,     /*!< step 10 error */
  I2C_ERR_STEP_11,     /*!< step 11 error */
  I2C_ERR_STEP_12,     /*!< step 12 error */
  I2C_ERR_START,       /*!< start error */
  I2C_ERR_ADDR10,      /*!< addr10 error */
  I2C_ERR_ADDR,        /*!< addr error */
  I2C_ERR_STOP,        /*!< stop error */
  I2C_ERR_ACKFAIL,     /*!< ackfail error */
  I2C_ERR_TIMEOUT,     /*!< timeout error */
  I2C_ERR_INTERRUPT,   /*!< interrupt error */
} i2c_status_type;
// I2C传输模式：
typedef enum{
  I2C_INT_MA_TX = 0,
  I2C_INT_MA_RX,
  I2C_INT_SLA_TX,
  I2C_INT_SLA_RX,
  I2C_DMA_MA_TX,
  I2C_DMA_MA_RX,
  I2C_DMA_SLA_TX,
  I2C_DMA_SLA_RX,
} i2c_mode_type;

// I2C控制参数：
typedef struct{
  i2c_type                               *i2cx;                   /*!< i2c registers base address      */
  uint8_t                                *pbuff;                  /*!< pointer to i2c transfer buffer  */
  __IO uint16_t                          pcount;                  /*!< i2c transfer counter            */
  __IO uint32_t                          mode;                    /*!< i2c communication mode          */
  __IO uint32_t                          timeout;                 /*!< i2c wait time                   */
  __IO uint32_t                          status;                  /*!< i2c communication status        */
  __IO i2c_status_type                   error_code;              /*!< i2c error code                  */
  dma_channel_type                       *dma_tx_channel;         /*!< dma transmit channel            */
  dma_channel_type                       *dma_rx_channel;         /*!< dma receive channel             */
  dma_init_type                          dma_init_struct;         /*!< dma init parameters             */
} i2c_handle_type;

#endif

void IIC_Pin_init(void);
void IIC_init(void);
//void I2C_RX_DMA_Init(void);
void I2C_TX_DMA_Init(void);
//void IIC_Write_str(uint8_t addr,char * pBuffer);
//void IIC_Write_arr(uint8_t addr);
//void I2C_Isr(void);

void i2c_dma_transmit_buffer(unsigned char *buffer, unsigned int length);
void i2c_wr_byte(unsigned char byte,unsigned char cmd);
void i2c_write_buffer(unsigned char *buffer,unsigned int length);
//void oled_reset(void);
//uint8_t i2c_read_byte(unsigned char add);

#endif


